define(['jquery', 'bootstrap'], function($, bootstrap){
    var common = {
        //监听表单提交
        submit: function(selector){
            $(document).on('submit', selector, function(event){
                //阻止当前事件冒泡
                event.preventDefault();
                //后端AJAX请求
                $.ajax({
                    type: 'POST',
                    cache: false,
                    url: $(this).attr('action'),
                    data: $(this).serialize(),
                    success: function(res,status,xhr) {
                        if(res.code == 1){
                            common.toastMini(res.msg);
                            common.location(res.url,500);
                        }else{
                            common.toastMini(res.msg);
                        }
                    },
                    error: function(xhr,status,errorThrown) {
                        common.toastMini(errorThrown);
                    }
                });
            });
        },
        //监听GET请求
        get: function(selector){
            $(document).on('click', selector, function(event){
                event.preventDefault();
                var element = $(this);
                $.ajax({
                    type: 'GET',
                    cache: false,
                    url: $(this).attr('href'),
                    timeout: 20000,
                    beforeSend: function(xhr){
                        element.append('<span class="spinner-border spinner-border-sm ms-1"></span>');
                    },
                    success: function(data,status,xhr) {
                        common.toastMini(data.msg);
                    },
                    error: function(xhr,status,errorThrown) {
                        common.toastMini(errorThrown);
                    },
                    complete: function(xhr,status){
                        element.find('span').remove();
                    }
                });
            });
        },
        //窗口提示
        modal: function(html){
            html = html || '请输入HTML代码';
            $('#modal').remove();
            $('body').append('<div class="modal fade" id="modal" data-bs-backdrop="static" data-bs-keyboard="false"><div class="modal-dialog modal-dialog-centered"><div class="modal-content text-center p-4 mb-0"><button type="button" class="btn-close position-absolute" data-bs-dismiss="modal" style="z-index:1;top:10px;right:10px;"></button><div>'+html+'</div></div></div></div>');
            new bootstrap.Modal($('#modal')).show();
        },
        //表单提示
        modalForm: function(html,size){
            html = html || '请输入HTML代码';
            size = size || 'modal-lg';
            $('#dialog').remove();
            $('body').append('<div class="modal fade" id="dialog" data-bs-backdrop="static" data-bs-keyboard="false"><div class="modal-dialog modal-dialog-centered '+size+'"><div class="modal-content p-4 mb-0"><button type="button" class="btn-close position-absolute" data-bs-dismiss="modal" style="z-index:1;top:10px;right:10px;"></button><div>'+html+'</div></div></div></div>');
            new bootstrap.Modal($('#dialog')).show();
        },
        //吐司提示
        toast: function(tips,timeout){
            tips = tips || 'ok';
            timeout = timeout || 3000;
            $('.toast-container').remove();
            $('body').append('<div class="toast-container position-fixed top-50 start-50 translate-middle"><div class="toast position-relative py-1" data-bs-delay="'+timeout+'"><p class="position-absolute" style="top:10px;right:10px;"><button class="btn-close"type="button" data-bs-dismiss="toast"></button></p><div class="toast-body text-center">'+tips+'</div></div></div>');
            new bootstrap.Toast($('.toast')).show();
        },
        //默认提示
        toastMini: function(tips,timeout){
            tips = tips || 'ok';
            timeout = timeout || 1000;
            $('.toast-container').remove();
            $('body').append('<div class="toast-container position-fixed top-50 start-50 translate-middle" style="width:250px"><div class="toast position-relative py-3 text-center border-0 bg-dark text-white" data-bs-delay="'+timeout+'">'+tips+'</div></div>');
            new bootstrap.Toast($('.toast')).show();
        },
        //延迟跳转
        location: function(url,timeout){
            timeout = timeout || 1000;
            if(url){
                setTimeout(function(){
                    top.location.href = url;
                },timeout);
            }
        },
        //json美化
        jsonBeauty: function(selector) {
            selector = selector || this.defaults.selector;
            $(selector).each(function() {
                var jsonText = $(this).val();
                if(jsonText){
                    $(this).val(JSON.stringify(JSON.parse(jsonText), null, 4));
                }
            });
        },
        //邮箱验证
        isEmail: function(email) {
            var emailRegExp = /^[a-zA-Z0-9_-]+@[a-zA-Z0-9_-]+(\.[a-zA-Z0-9_-]+)+$/;
            return emailRegExp.test(email);
        },
        //手机验证
        isMobile: function(mobile){
            var reg = /^(0|86|17951)?(13|14|15|16|17|18|19)[0-9]{9}$/;
            return mobile.match(reg);
        }
    };
    //固定格式
    return common;
});